package com.qfedu.empproject.controller;

import com.qfedu.empproject.entity.Emp;
import com.qfedu.empproject.service.EmpService;
import com.qfedu.empproject.service.impl.EmpServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/EditEmp")
public class EditEmpAndShowController extends HttpServlet {
    private EmpService empService = new EmpServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取员工编辑页面传递过来的员工信息 name/salary/age/id
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String salary = req.getParameter("salary");
        String age = req.getParameter("age");
        // 构造一个员工Emp对象
        Emp emp = new Emp(Integer.valueOf(id), name, Double.valueOf(salary), Integer.valueOf(age));
        // 调用业务层service执行更新方法
        int update = empService.update(emp);
        if (1 == update) {
            // 更新成功，跳转到列表页面 转发
            req.getRequestDispatcher("/showAll").forward(req, resp);
        }
    }
}
